package tata.invoker.controller;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tata.ApiFacade;
import tata.Father;
import tata.GenericType;
import tata.R.Response;
import tata.StreamFacade;

import java.time.LocalDateTime;

@RestController
@RequestMapping("ping")
public class PingController {

    @DubboReference
    private StreamFacade streamFacade;
//    @DubboReference(url = "tri://localhost:50051")
    @DubboReference
    private ApiFacade apiFacade;

    @GetMapping("pone")
    public Person pone(){
        Person person = new Person();
        person.setMyName("鸟人");
        person.setCreateTime(LocalDateTime.now());
        return person;
    }
    @GetMapping("api")
    public Response<Father> api(){
        Response<Father> father = apiFacade.father();
        return father;
    }

    @GetMapping("testStream")
    public String testStream(){
        streamFacade.serverStream("dfs",new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.out.println(data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("onCompleted");

            }
        });
        return "testStream";
    }
    @GetMapping("serverStreamGeneric")
    public String serverStreamGeneric(){
        streamFacade.serverStreamGeneric("dfs",new StreamObserver<>() {

            @Override
            public void onNext(GenericType<String> data) {
                System.out.println(data.data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }
        });
        return "testStream";
    }
}
